package com.qdxwx.manage.controller.dict;

import com.qdxwx.common.annotation.Permissions;
import com.qdxwx.common.entity.Ztree;
import com.qdxwx.common.json.AjaxResult;
import com.qdxwx.common.page.PageRequest;
import com.qdxwx.common.page.TableDataInfo;
import com.qdxwx.data.service.dict.DictTypeService;
import com.qdxwx.manage.annotation.Log;
import com.qdxwx.manage.controller.BaseController;

import com.qdxwx.manage.support.SessionUtils;
import com.qdxwx.models.dict.DictType;
import com.qdxwx.models.enums.BusinessType;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 数据字典信息
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Controller
@RequestMapping("/dict")
public class DictTypeController extends BaseController
{
    private String prefix = "dict/type";

    @Autowired
    private DictTypeService dictTypeService;

    @Permissions("dict:list")
    @GetMapping("list.html")
    public String dictType()
    {
        return prefix + "/type";
    }

    @PostMapping("/list.json")
    @Permissions("dict:list")
    @ResponseBody
    public TableDataInfo list(HttpServletRequest request, DictType dictType)
    {
        List<DictType> list = dictTypeService.getList(dictType, PageRequest.form(request));
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(list);
        rspData.setTotal(dictTypeService.count(dictType));
        return rspData;
    }
    /**
     * 新增字典类型
     */
    @GetMapping("/add.html")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存字典类型
     */
    @Log(title = "字典类型", businessType = BusinessType.INSERT)
    @Permissions("dict:add")
    @PostMapping("/add.json")
    @ResponseBody
    public AjaxResult addSave(@Validated DictType dict)
    {
        if (dictTypeService.isExist(dict))
        {
            return error("新增字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
        dict.setCreateBy(SessionUtils.getLoginName());
        return toAjax(dictTypeService.insert(dict));
    }

    /**
     * 修改字典类型
     */
    @GetMapping("/edit/{dictId}")
    public String edit(@PathVariable("dictId") Long dictId, ModelMap mmap)
    {
        mmap.put("dict", dictTypeService.getById(dictId));
        return prefix + "/edit";
    }

    /**
     * 修改保存字典类型
     */
    @Log(title = "字典类型", businessType = BusinessType.UPDATE)
    @Permissions("dict:edit")
    @PostMapping("/edit.json")
    @ResponseBody
    public AjaxResult editSave(@Validated DictType dict)
    {
        if (dictTypeService.isExist(dict))
        {
            return error("修改字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
        dict.setUpdateBy(SessionUtils.getLoginName());
        return toAjax(dictTypeService.update(dict));
    }

    @Log(title = "字典类型", businessType = BusinessType.DELETE)
    @Permissions("dict:remove")
    @PostMapping("/remove.json")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(dictTypeService.deleteByIds(ids));
    }

    /**
     * 清空缓存
     */
    @Permissions("dict:remove")
    @Log(title = "字典类型", businessType = BusinessType.CLEAN)
    @GetMapping("/clearCache.json")
    @ResponseBody
    public AjaxResult clearCache()
    {
        dictTypeService.clearCache();
        return success();
    }



    /**
     * 校验字典类型
     */
    @PostMapping("/checkDictTypeUnique.json")
    @ResponseBody
    public String checkDictTypeUnique(DictType dictType) {
        return dictTypeService.isExist(dictType) ? "1" : "0";
    }

    /**
     * 选择字典树
     */
    @GetMapping("/selectDictTree/{columnId}/{dictType}")
    public String selectDictTree(@PathVariable("columnId") Long columnId, @PathVariable("dictType") String dictType,
            ModelMap mmap)
    {
        mmap.put("columnId", columnId);
        mmap.put("dict", dictTypeService.getByType(dictType));
        return prefix + "/tree";
    }

    /**
     * 加载字典列表树
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData()
    {
        List<Ztree> ztrees = dictTypeService.getTree(new DictType());
        return ztrees;
    }
}
